var request = null;

try{
	request = new XMLHttpRequest();
}catch (trymicrosoft){
	try{
		request = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(othermicrosoft){
		try{
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(failed){
			request = null;
		}
	}
}

if(request == null)
	alert("Error Creating request Object!");

function validateProjName(){
	var projName = document.getElementById("projectName").value;
	var url = "../cgi-bin/interface/validateProjName.cgi?projectName="+escape(projName);
	request.onreadystatechange = showValidateProjName;
	request.open("GET",url,true);
	request.send(null);
}

function showValidateProjName(){
	if(request.readyState == 4){
		if(request.status == 200){
			var response = request.responseText;
			if(response == 0){
				var projName = document.getElementById("projectName").value;
				var message = projName+" already existed, please choose another project name.";
				document.getElementById("projectName").value = '';
				document.getElementById("projectName").focus();
				alert(message);
			}
		}
	}
}

function validateRegEx(regex, input, helpText, helpMessage) {
        // See if the input data validates OK
        if (!regex.test(input)) {
          // The data is invalid, so set the help message and return false
          if (helpText != null)
            helpText.innerHTML = helpMessage;
          return false;
        }else {
          // The data is OK, so clear the help message and return true
          if (helpText != null)
            helpText.innerHTML = "";
          return true;
        }
}

function validateNonEmpty(inputField, helpText) {
// See if the input value contains any text
return validateRegEx(/.+/,
        inputField.value, helpText,
        "Please enter a value.");
}


function validateEmail(inputField, helpText) {
        // First see if the input value contains data
	if (!validateNonEmpty(inputField, helpText))
	        return false;

	// Then see if the input value is an email address
	return validateRegEx(/^[\w\.-_\+]+@[\w-]+(\.\w+)+$/,
	        inputField.value, helpText,
	        "Please enter a valid email address.");
}


